
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionHelper {

  // 首页打开获取权限
  static Future<bool> requestAllPermission() async {
    List<Permission> permissionList = [
    ];
    if (Platform.isAndroid) {
      var androidInfo = await DeviceInfoPlugin().androidInfo;
      String? release = androidInfo.version.release;
      String version;
      if(release != null){
        version = release;
        if(release.contains(".")){
          version = release.substring(0,release.indexOf("."));
        }
        if (kDebugMode) {
          print('version：' + version);
        }
        if (10 >= int.parse(version)) {
          permissionList.add(Permission.storage);
        } else if(13 > int.parse(version) && int.parse(version) >= 11){
          permissionList.add(Permission.manageExternalStorage);
        } else if (int.parse(version) >= 13){
          permissionList.add(Permission.manageExternalStorage);
          permissionList.add(Permission.photos);
          permissionList.add(Permission.videos);
          permissionList.add(Permission.audio);
        }
      }
    }
    List<bool> results = [];
    for (var permission in permissionList) {
      PermissionStatus status = await permission.request();
      if (Platform.isAndroid) {
        results.add(status == PermissionStatus.granted);
      }
    }
    return results.contains(false);
  }

  static Future<bool> requestPermission(List<Permission> permissionList) async {
    List<bool> results = [];
    for (var permission in permissionList) {
      PermissionStatus status = await permission.request();
      if (Platform.isAndroid) {
        results.add(status == PermissionStatus.granted);
      }
    }
    return !results.contains(false);
  }

}